﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using SsoaFramework.Core.Other;

namespace SsoaFramework.Core
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class EnterpriseServiceBus
    {
        private readonly ServiceRegistry serviceRegistry;
        private readonly ServiceRouter serviceRouter;
        private readonly ExposeServiceBus exposeServiceBus;

        public ServiceRegistry ServiceRegistry { get { return serviceRegistry; } }
        public ServiceRouter ServiceRouter { get { return serviceRouter; } }
        public ExposeServiceBus ExposeService { get { return exposeServiceBus;  } }

        public EnterpriseServiceBus(Uri address)
        {
            serviceRouter   = new ServiceRouter(address, this);
            serviceRegistry = serviceRouter.ServiceRegistry;
            exposeServiceBus = new ExposeServiceBus(this);
        }

        public void Start()
        {
            Log.Instance.Info("ESB is starting");
            serviceRouter.Start();
            serviceRegistry.Start();
            exposeServiceBus.Start();
            Log.Instance.Info("ESB has started");
        }

        public void Stop()
        {
            Log.Instance.Info("ESB is stopping");
            serviceRegistry.StopSearching();
            serviceRouter.Stop();
            exposeServiceBus.Stop();
            Log.Instance.Info("ESB has stopped");
        }
    }
}
